package services

import (
	"Bok/daos"
	"Bok/models"
	"fmt"
)

type BlogService interface {
	CreateBlogTypeService(blogType *models.BlogTypeRequest) error
	EditBlogTypeService(id uint, blogType *models.BlogTypeRequest) error
	DeleteBlogTypeService(ids []uint) error
	GetBlogTypeService() ([]*models.BlogTypeResponse, error)
}

type blogService struct {
	blogDao daos.BlogDao
}

func NewBlogService(blogDao daos.BlogDao) BlogService {
	return &blogService{blogDao: blogDao}
}

func (c *blogService) CreateBlogTypeService(blogType *models.BlogTypeRequest) error {
	// 查找有没有重名的
	fmt.Println("service:blogType:", blogType)
	data, err := c.blogDao.FindBlogTypeByName(blogType.Name)
	if err != nil {
		return err
	}
	if data != 0 {
		return fmt.Errorf("已存在")
	}
	if err := c.blogDao.CreateBlogTypeDao(blogType); err != nil {
		return err
	}
	return nil
}
func (c *blogService) EditBlogTypeService(id uint, blogType *models.BlogTypeRequest) error {
	if err := c.blogDao.EditBlogTypeDao(id, blogType); err != nil {
		return err
	}
	return nil
}

func (c *blogService) DeleteBlogTypeService(ids []uint) error {
	if err := c.blogDao.DeleteBlogTypeDao(ids); err != nil {
		return err
	}
	return nil
}

func (c *blogService) GetBlogTypeService() ([]*models.BlogTypeResponse, error) {

	data, err := c.blogDao.GetBlogTypeList()
	if err != nil {
		return nil, err
	}
	return data, nil

}
